var app = new Vue({
	el: '#app',
	data: {
		oldpassword: '',
		newpassword: '',
		newname: '',
		adminMsg: {
			isLogin: false,
			id: null,
			account: null,
			name: null,
			headimg: null
		},
		modulesMsg: [],
		contentMsg: {
			title: null,
			coverimg: null,
			module_id: null
		},
		editor: null
	},
	watch: {
	},
	methods: {
		changePassword: function(){
			var self = this;
			var id = self.adminMsg.id;
			var oldpassword = self.oldpassword;
			var newpassword = self.newpassword;
			var data = {
				"id": id,
				"oldpassword": oldpassword,
				"newpassword": newpassword
			}
			$.post("/api/admin/changePassword", data, function(data,status){
				if (data.success == "ok") {
					$("#passwordModal").modal("hide");
					swal("修改成功","","success");
				} else {
					swal("修改失败",data.msg, "error");
				}
			})
		},
		changeName: function(){
			var self = this;
			var id = self.adminMsg.id;
			var name = self.newname;
			var data = {
				"id": id,
				"name": name
			}
			$.post("/api/admin/setAdmin", data, function(data,status){
				if (data.success == "ok") {
					$("#nameModal").modal("hide");
					self.getLoginStatus();
					swal("修改成功","","success");
				} else {
					swal("修改失败",data.msg, "error");
				}
			})
		},
		logout: function(){
			var self = this;
			swal({ 
				title: "确定退出吗？", 
				type: "warning",
				showCancelButton: true, 
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定", 
  				cancelButtonText: "取消"
			},function(){
				$.post("/api/admin/logout",{},function(data,status){
					if (data.success = "ok") {
						self.getLoginStatus();
					}
				});
			});
		},
		getLoginStatus: function(){
			var self = this;
			$.get('/api/admin/isLogin',function(data,status){
				if (data.success == "ok") {
					self.adminMsg.isLogin = true;
					self.adminMsg.name = data.result.name;
					self.adminMsg.id = data.result.id;
					self.adminMsg.account = data.result.account;
					self.adminMsg.headimg = data.result.headimg;
				} else {
					self.adminMsg.isLogin = false;
					window.location.href = "/admin/login.html"
				}
			})
		},
		initEditor: function(){
			var editor = new Simditor({
				textarea: $('#editor'),
				toolbar: [
					'title',
					'bold',
					'italic',
					'underline',
					'strikethrough',
					'fontScale',
					'color',
					'ol',
					'ul',
					'blockquote',
					'code' ,
					'table',
					'link',
					'image',
					'hr',
					'indent',
					'outdent',
					'alignment'
				],
				tabIndent: false,
				upload: {
					url: '/api/upload',
					params: null,
					fileKey: 'imgfile',
					connectionCount: 1,
					leaveConfirm: "未上传完毕，请不要离开"
				}
			});
			this.editor = editor;
		},
		getModuleMsg: function(){
			var self = this;
			$.get('/api/admin/getModule',function(data,status){
				if (data.success == "ok") {
					self.modulesMsg = data.result;

				} else {
					swal("发生错误","可能网络出现了问题...","error");
				}
			})
		},
		uploadCover: function(){
			var self = this;
			var formData = new FormData($("#uploadForm")[0]);
			$.ajax({    
				url: '/api/upload' ,  /*这是处理文件上传的servlet*/  
				type: 'POST',    
				data: formData,    
				async: false,    
				cache: false,    
				contentType: false,    
				processData: false,    
				success: function (result) {
					if (result.success == true) {
						self.contentMsg.coverimg = result.file_path;
					}
				},    
				error: function (result) {    
					alert(result);    
				}    
			});  
		},
		publish: function(){
			console.log(this.contentMsg);
			console.log(this.editor.getValue());
			var self = this;
			var data = {
				"author_id": self.adminMsg.id,
				"module_id": self.contentMsg.module_id,
				"title": self.contentMsg.title,
				"coverimg": self.contentMsg.coverimg,
				"content": self.editor.getValue()
			}

			$.post('/api/admin/createNews',data, function(data,status){
				if (data.success == "ok") {
					swal("发表成功","请前往首页查看","success");
				} else {
					swal("发生错误","可能网络出现了问题...","error");
				}
			})
		}
	},
	mounted: function(){
		var self = this;
		this.getLoginStatus();
		this.initEditor();
		this.getModuleMsg();
		$("#uploadForm").change(function(){
			self.uploadCover();
		})
	}
})
